package edu.mit.media.fv.aqwidgets
{
	import edu.mit.media.fv.blocks.*;
	import edu.mit.media.fv.components.*;
	import edu.mit.media.fv.events.*;
	import edu.mit.media.fv.fluidspace.*;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.utils.Timer;	
	public class AQMainTray extends MovieClip
	{
		private static const DEFAULT_WIDTH:int = 670;
		private static const DEFAULT_HEIGHT:int = 400;
		private static const PARTITIONS:int = 5;
		
		private var viewInLimbo:DisplayObject;
		private var view:DisplayObject = null;
		private var timer:Timer;
		private var lock:Boolean = false;
		
		public function AQMainTray(){
			super();
			
			this.timer = new Timer(25, PARTITIONS);
			this.timer.addEventListener(TimerEvent.TIMER, onTimer);
			this.timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
			
			
//			var _mask:Sprite = new Sprite();
//			_mask.graphics.beginFill(0xFFFFFF);
//			_mask.graphics.moveTo(0,75)
//			_mask.graphics.lineTo(625,75)
//			_mask.graphics.lineTo(625,0)
//			_mask.graphics.lineTo(720,0)
//			_mask.graphics.lineTo(720,480)
//			_mask.graphics.lineTo(625,480)
//			_mask.graphics.lineTo(625,405)
//			_mask.graphics.lineTo(0,405)
//			_mask.graphics.lineTo(0,75)
//			_mask.graphics.endFill();
//			this.addChild(_mask);
//			this.mask = _mask;
			
			FluidEventHandler.addEventListener(WidgetEvent.DISPLAY_WIDGET, displayWidget);
		}
		public function onTimer(event:TimerEvent):void{
			if(! this.contains(this.viewInLimbo)){
				this.addChild(this.viewInLimbo);
			}
			if(this.viewInLimbo != null && !this.lock){
				this.viewInLimbo.x = (timer.repeatCount-timer.currentCount)*DEFAULT_WIDTH/PARTITIONS;
			}else{
				trace("View In Limbo is null");
			}
		}
		public function onTimerComplete(event:TimerEvent):void{
			this.lock = true;
			
			if(this.view != null){
				this.removeChild(this.view);
			}
			if(! this.contains(this.viewInLimbo)){
				this.addChild(this.viewInLimbo);
			}
			this.view = this.viewInLimbo;
			this.viewInLimbo.x = 0;
			this.lock = false;
		}
		public function displayWidget(event:WidgetEvent):void{
			this.setView(event.widget);
		}
		public function setView(view:DisplayObject):void{
			if(view != null && !this.lock){
				this.viewInLimbo = view;
				timer.reset();
				timer.start();
			}
		}
	}
}